package yl.hs.bmipfull.security;

import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import yl.hs.bmipfull.utils.ResultCode;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.viewmodels.WebResult;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
 * SecurityAccessDeniedHandler
 * @category
 * @author 谢景广
 * @date 2021-05-26
 */
public class SecurityAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {

        if (e != null) {
            var webResult = new WebResult(ResultCode.UNAUTHOR, "需要授权", request.getRequestURL().toString());
            Tl.writeObjectToResponse(response, webResult);
        }
    }
}
